package net.entelijan.cobean.examples.lit;

import java.awt.event.ActionEvent;

import edu.umd.cs.findbugs.annotations.SuppressWarnings;

import net.entelijan.cobean.data.literature.ILitService;
import net.entelijan.cobean.data.literature.LitAuthor;
import net.entelijan.cobean.examples.util.ExamplesUtil;
import net.entelijan.config.CobeanConfigFactory;
import net.entelijan.config.ICobeanConfig;

public class EditAuthorDialogController {

	private LitAuthor model;
	private EditAuthorDialog view;
	private ILitService litService;
	private IEditAuthorDialogParent parent;

	@SuppressWarnings
	public EditAuthorDialogController(LitAuthor model, EditAuthorDialog view,
			ILitService litService, IEditAuthorDialogParent parent) {
		this.model = model;
		this.view = view;
		this.litService = litService;
		this.parent = parent;
		addListeners(view);
		ICobeanConfig cfg = CobeanConfigFactory.createConfig();
		this.model = cfg.bind(this.model, view, null);
	}

	private void addListeners(EditAuthorDialog view) {
		view.getOkButton().addActionListener(
				new DefaultActionListener("Confirm Edit Author") {

					@Override
					public void actionPerformedIntern(ActionEvent e) {
						EditAuthorDialogController.this.litService
								.saveAuthor(EditAuthorDialogController.this.model);
						ExamplesUtil.disposeRoot(e);
						parent.refreshAuthorList();
					}

				});
		view.getCancelButton().addActionListener(
				new DefaultActionListener("Cancel Edit Author") {

					@Override
					public void actionPerformedIntern(ActionEvent e) {
						ExamplesUtil.disposeRoot(e);
					}
				});
	}

	public void open() {
		view.setVisible(true);
	}

}
